﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TurrotHR : MonoBehaviour
{
    private GameObject targetObj;
    public bool isRun;
    private bool isOnlyOne = false;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(isRun)
        {
            //为了不重复开启协成
            if (!isOnlyOne)
            {
                StartCoroutine(WaitForSecondsToDo(0.1f));   //重复开启
                isOnlyOne = true;
            }

            if (targetObj == null)
                return;
            Vector3 direction = targetObj.transform.position - transform.position;
            //print(direction);
            direction = Quaternion.AngleAxis(90, Vector3.up) * direction;   //由于模型默认对齐不准确的关系必须将向量再旋转90度才是正确的
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), 0.1f);
        }
        else
        {
            if (isOnlyOne)
            {
                StopAllCoroutines();    //重复关闭
                isOnlyOne = false;
            }
        }
    }

    IEnumerator WaitForSecondsToDo(float t)
    {
        while(true)
        {
            //print("isRun");
            yield return new WaitForSeconds(t);
            targetObj = GameObject.FindGameObjectWithTag("Enemy");
            //print(targetObj != null);
        }
    }
}
